/**
 * Created by liuqingxia on 2016/2/14.
 */

var url = getUrlBySuffix("/job/find");//任务查询后台地址
var removeJobId;//要删除的任务的id

var cronBtn = "" +
    '<a href="javascript:;" class="start-btn">' +
    '<span class="label label-important" style="font-size: 14px">' +
    '<i class="fa fa-upload"></i>&nbsp;&nbsp;' +
    '提交' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="start-now-btn">' +
    '<span class="label label-warning-light" style="font-size: 14px">' +
    '<i class="fa fa-play"></i>&nbsp;&nbsp;' +
    '立即执行' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="pause-btn">' +
    '<span class="label label-danger" style="font-size: 14px">' +
    '<i class="fa fa-pause"></i>&nbsp;&nbsp;' +
    '暂停' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="resume-btn">' +
    '<span class="label label-default" style="font-size: 14px">' +
    '<i class="fa fa-refresh"></i>&nbsp;&nbsp;' +
    '恢复' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="interrupt-btn">' +
    '<span class="label label-info" style="font-size: 14px">' +
    '<i class="fa fa-stop"></i>&nbsp;&nbsp;' +
    '终止' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="history-btn">' +
    '<span class="label label-warning" style="font-size: 14px">' +
    '<i class="fa fa-list-alt"></i>&nbsp;&nbsp;' +
    '明细' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="update-btn">' +
    '<span class="label label-success" style="font-size: 14px">' +
    '<i class="fa fa-edit"></i>&nbsp;&nbsp;' +
    '编辑' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="remove-btn">' +
    '<span class="label label-primary" style="font-size: 14px">' +
    '<i class="fa fa-trash-o"></i>&nbsp;&nbsp;' +
    '删除' +
    '</span>' +
    '</a>&nbsp;';

var onceBtn = "" +
    '<a href="javascript:;" class="start-now-btn">' +
    '<span class="label label-info" style="font-size: 14px">' +
    '<i class="fa fa-play"></i>&nbsp;&nbsp;' +
    '执行' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="history-btn">' +
    '<span class="label label-warning" style="font-size: 14px">' +
    '<i class="fa fa-list-alt"></i>&nbsp;&nbsp;' +
    '明细' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="update-btn">' +
    '<span class="label label-success" style="font-size: 14px">' +
    '<i class="fa fa-edit"></i>&nbsp;&nbsp;' +
    '编辑' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="interrupt-btn">' +
    '<span class="label label-info" style="font-size: 14px">' +
    '<i class="fa fa-stop"></i>&nbsp;&nbsp;' +
    '终止' +
    '</span>' +
    '</a>&nbsp;' +
    '<a href="javascript:;" class="remove-btn">' +
    '<span class="label label-primary" style="font-size: 14px">' +
    '<i class="fa fa-trash-o"></i>&nbsp;&nbsp;' +
    '删除' +
    '</span>' +
    '</a>&nbsp;';

/**
 * 对当前对象（任务，项目组，用户）的各种操作（修改，删除等，以url标识）
 * @param url
 */
function operationByUrl(url, ele,ltsTable) {
    var params = {};
    params['id'] = $.trim(getCurrentId(ele));
    $.postHttp(params, getUrlBySuffix(url), function(json){
    	if (json && json.success) {
    		swal({
				title : "操作成功",
				text : json['message'],
				type : "success",
				confirmButtonText : "确定",
				timer:1000
			}, function() {
				var param = getFormParams("job-find-form");
            	ltsTable.post(param, pageNum); 
			});
            
        } else {
            if (json) {
                swal("失败", json['message'], 'error');
            }
        }
    });
}

/**
 * 根据id获取任务对象
 * @param id
 */
function getJobById(id) {
    var params = {};
    params['id'] = id;
    $.postHttpSync(params, getUrlBySuffix("/job/findById"), function (json) {
        if (json && json.success) {
            var list = json['list'];
            params = list[0];
        } else {
            if (json) {
                swal('失败', json['message'], 'error');
                return;
            }
        }
    });
    return params;
}

/**
 * 根据给定job对象，
 * 把job对象的个属性值填充到编辑表格中
 * @param job
 */
function fillJobEditForm(job) {

    var executeTypeInt = job['executeType'];
    initExecuteTypeRadio(executeTypeInt);
    $.each($('#editForm').parent().find(".form-control"), function () {
        var name = $(this).attr("name");
        if (isNull(name)) {
            return true;
        }
        name = $.trim(name);
        if (name == "state") {
            var state = job['state'];
            var result = switchStateToCN(state);
            $(this).val(result);
            return true;

        }
        var value = job[name];
        $(this).val(value);
    });
}

/**
 * 根据state值转换成对应的提示信息
 * @param state
 * @returns {*}
 */
function switchStateToCN(state) {
    var result;
    switch (state) {
        case 0:
            result = "执行成功";
            break;
        case 1:
            result = "提交成功";
            break;
        case 2:
            result = "初始化";
            break;
        case 3:
            result = "运行中";
            break;
        case 4:
            result = "执行失败";
            break;
        case 5:
            result = "被终止";
            break;
        case 6:
            result = "暂停";
            break;
        default :
            result = "状态未知";
            break;
    }
    return result;
}

/**
 * 将state转换成对应的数字
 * @param state
 * @returns {*}
 */
function switchStateToNumber(state) {
    var result;
    switch (state) {
        case "执行成功":
            result = 0;
            break;
        case "提交成功":
            result = 1;
            break;
        case "初始化":
            result = 2;
            break;
        case "运行中":
            result = 3;
            break;
        case "执行失败":
            result = 4;
            break;
        case "被终止":
            result = 5;
            break;
        case "暂停":
            result = 6;
            break;
        default :
            result = 255;
            break;
    }
    return result;
}

/**
 * 获取Job表单中的值，
 * 在这个过程中把state转换成数字
 * @param id
 */
function getJobFormParams(id) {
    var params = getFormParams(id);
    var result = $.trim(params['state']);
    params['state'] = switchStateToNumber(result);
    if (!getExecuteTypeParams(params)) {
        return;
    }
    //console.log(JSON.stringify(params));
    return params;
}

/**
 * 点击job编辑框上的取消按钮
 */
function clickJobEditCancelBtn() {
    $(document).on("click", "#job-edit-modal-dismiss", function () {
        $("#searchBtn").trigger("click");
    });
}